let util = require('../../utils/util');
const api = require('../../utils/api');
var wxParse = require('../../wxParse/wxParse.js');
let app = getApp();
Page({
  data: {

    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    duration: 1000,
    pruInfo: '',
    wujiaoxing: "../../assets/img/wujiaoxing2.png",
    productId: '',
  },
  changeIndicatorDots: function(e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function(e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function(e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function(e) {
    this.setData({
      duration: e.detail.value
    })
  },
  //收藏按钮
  collect() {
    let that = this;
    wx.showLoading({
      title: '',
    })
    util.myRequest(
      api.collectaddordelete, {
        valueId: that.curGoodsId.toString(),
        typeId: "1"
      },
      res => {
        console.log(res);
        if (res.data.errno == 0) {
          let title = res.data.data.type == "add" ? "收藏成功！" : '取消收藏！'
          let icon = res.data.data.type == "add" ? "success" : 'none'
          wx.showToast({
            title,
            icon
          });
        }
        if (res.data.data.type == "add") {
          this.setData({
            wujiaoxing: "../../assets/img/wujiaoxing.png"
          })
        } else {
          this.setData({
            wujiaoxing: "../../assets/img/wujiaoxing2.png"
          })
        }
      },
      'POST'
    )
  },
  //进入购物车
  shopCar() {
    wx.switchTab({
      url: '../../pages/shopCar/shopCar',
    })
  },
  //客服聊天
  kefu(e) {
    let that = this;
    app.global.isProductInfo = true;
    wx.setStorageSync('goodsId', that.curGoodsId);
    wx.switchTab({
      url: '../../pages/onlineQuery/onlineQuery',
      success() {}
    })
    // wx.navigateTo({
    //   url: '../../pages/onlineQuery/onlineQuery?id=' + this.curGoodsId,
    // })
  },
  //添加商品到购物车
  addCar() {
    let that = this;
    util.myRequest(
      api.cartadd, {
        goodsId: that.curGoodsId,
        number: '1',
        productId: that.data.productId
      },
      res => {
        console.log(res)
        if (res.data.errmsg == "库存不足") {
          wx.showToast({
            title: '库存不足',
            icon: "none"
          })
        } else {
          wx.showToast({
            title: '添加成功！',
          })
        }
      },
      'POST'
    )
  },
  onLoad: function(opation) {
    this.curGoodsId = opation.id;
    app.global.goodsId = opation.id;
    console.log(opation)
    //商品详情ajax请求
    let that = this;
    wx.showLoading({
      title: '正在加载',
    })
    util.myRequest(
      api.goodsdetail, {
        id: opation.id
      },
      res => {
        console.log(res);
        if (res.data.data.gallery.length == '0') {
          wx.showToast({
            title: '该商品已下架',
            icon: "none"
          })
          setTimeout(function() {
            wx.navigateBack({
              delta: 1
            });
          }, 1000)
        } else {
          wxParse.wxParse('article', 'html', res.data.data.info.goods_desc, this);
          that.setData({
            pruInfo: res.data.data,
            productId: res.data.data.productList[0].id,

          })
          wx.hideLoading({
            title: '加载完成'
          })
          // console.log(res.data.data.info.goods_desc)
        }

      }
    )

  }
})